<?php

namespace app\admin\controller;

use app\common\Helper;

use app\admin\model\Advertiselocal as AdvertiselocalMdl;
use app\admin\model\Advertise;
use app\admin\model\Advertiselocalconnect;

use think\Db;
use think\Exception;
use think\Request;

class Advertiselocal extends Base
{

    public function getlist(){
        $params = $this->request->param();
        $where = [
            'status' => ['neq', 2]
        ];
        if(isset($params['advertiseLocalName']) && $params['advertiseLocalName']){
            $where['advertiseLocalName'] = ['like', "%{$params['advertiseLocalName']}%"];
        }

        $model = model('Advertiselocal');
        $list = $model->where($where)->order('advertiseLocalNumb ASC')->paginate(10);
        $total = $model->where($where)->count();

        return $this->fetch('',[
            'datas' => $list,
            'total' => $total,
            'formget' => $_GET
        ]);
    }

    public function add(){
        return $this->fetch();
    }

    public function edit(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return show(config('status.error_code'),'缺少参数','缺少参数');
        }
        $info = AdvertiselocalMdl::get($param['id']);

        return $this->fetch('',[
            'info' => $info
        ]);
    }

    public function savePost(Request $request){
        if($request->isPost()){
            $param = $request->param();

            if(empty($param['id'])){
                //添加
                $model = new AdvertiselocalMdl();
                $id = Helper::getUUID();
                $model->id = $id;
                // 广告位位置编号
                $maxNum = AdvertiselocalMdl::where('type', $param['type'])->max('advertiseLocalNumb');
                $maxNum = $maxNum ? $maxNum : 0;
                $model->advertiseLocalNumb = $maxNum + 1;
            }else{
                //修改
                $model = AdvertiselocalMdl::get($param['id']);
                if(!$model){
                    return show(config('status.error_code'),'数据异常','数据异常');
                }
            }
            $model->advertiseLocalName = $param['advertiseLocalName'];
            $model->topimgurl = $param['topimgurl'];
            $model->type = $param['type'];
            $model->advertiseForCharge = $param['advertiseForCharge'] * 100;
            $result = $model->save();

            if($result !== false){
                return show(config('status.success_code'),'保存成功','保存成功');
            }else{
                return show(config('status.error_code'),'保存失败','保存失败');
            }
        }else{
            return show(config('status.error_code'),'请求不合法','请求不合法');
        }
    }

    /**
     * 删除
     */
    public function del(Request $request){
        $model = model($this->model);
        if($request->isPost()){
            //$param = $request->param();
            $id = $request->param('id');

            Db::startTrans();
            try {
                // 是否有投放中的广告
                $advertiseIds = Advertiselocalconnect::where('advertiseLocalId', $id)->field('advertiseId')->select();
                $advertiseIds = array_column($advertiseIds, 'advertiseId');
                $advertise = Advertise::whereIn('id', $advertiseIds)->where(['status' => 1, 'endTime' => ['gt', date('Y-m-d H:i:s')]])->find();
                if($advertise){
                    Db::rollback();
                    return show(config('status.error_code'),'该广告位有投放中的广告，不能删除','该广告位有投放中的广告，不能删除');
                }

                $result = $model->where('id', $id)->update(['status' => 2]);

                if($result === false){
                    Db::rollback();
                    return show(config('status.error_code'),'删除失败','删除失败');
                }

                /*
                // 删除广告
                //Advertise::whereIn('id', $advertiseIds)->save(['status' => 3]);
                Advertise::whereIn('id', $advertiseIds)->delete();
                // 删除广告广告位关联数据
                Advertiselocalconnect::where('advertiseLocalId', $id)->delete();
                */

                Db::commit();
                return show(config('status.success_code'),'删除成功','删除成功');
            } catch (Exception $e) {
                Db::rollback();
                return show(config('status.error_code'), '删除失败', $e->getMessage());
            }
        }
    }

}